touch — Create Files & Update Timestamps Linux coreutils
touch is the fastest way to create an empty file or refresh a file’s timestamps (access time and modification time).
On a VPS, it’s commonly used to create log placeholders, trigger build/deploy systems that watch timestamps, and prevent scripts from failing with “file not found”.
Use touch when you need a file to exist, or when you need a timestamp change without changing file content.
touch does not erase filesRunning touch existing-file does not delete or clear content. It only updates timestamps (unless you use special flags to target access/modify time).
What You’ll Learn
- Create new empty files (single or many)
- Update access time and/or modification time
- Set custom timestamps (
-t,-d) and copy timestamps (-r) - Use
touchin WordPress/VPS workflows (logs, placeholders, scripts) - Avoid common mistakes (wrong date format, missing directories)
Core Syntax
touch [OPTION]... FILE...
- You can pass multiple files in one command.
- With wildcards, you can update many files at once.
Options That Matter
| Option | Meaning | Typical Use |
|---|---|---|
| -- | - | - |
-c | Do not create file if missing | Update timestamps only (no new files) |
-a | Change access time only | Simulate “read” activity |
-m | Change modification time only | Trigger rebuild/cache refresh |
-t [[CC]YY]MMDDhhmm[.ss] | Set a specific timestamp | Testing, compliance, reproducibility |
-d DATE | Use a human-readable date string | Easier than -t formatting |
-r FILE | Copy timestamp from reference file | Align times between related files |
--help | Show help | Offline reference |
--version | Show version | Compatibility checks |
- Prefer
-dfor readability ("2025-09-30 14:00:00") - Use
-twhen you want a strict numeric format (script-friendly)
Common Patterns
- Create file(s)
- Update mtime only
- Update only (don’t create)
- Custom timestamp
touch test.txt
touch file1.txt file2.txt file3.txt
touch -m style.css
touch -c maybe-missing.log
touch -d "2025-09-30 14:00:00" error.log
Best Practices
-
Use
-cwhen you want to avoid accidentally creating new files. -
Verify changes with
ls -lorstatafter touching files. -
Don’t confuse
touchwithmkdir(touch is for files, not directories). -
If a path includes directories that might not exist, create them first:
mkdir -p /var/www/html/wp-content/uploads
Preview first:
ls *.php
Then touch:
touch *.php
Troubleshooting
| Problem | Likely Cause | Fix |
|---|---|---|
| - | ||
| File not created | Parent directory doesn’t exist | Create directory first (mkdir -p ...) |
Permission denied | No rights in target path | Use sudo or fix permissions |
| Wrong timestamp | Incorrect -t format | Use -d "YYYY-MM-DD HH:MM:SS" |
| “Did touch overwrite my file?” | Misunderstanding | touch does not erase content; only updates timestamps |
Verification commands (HTML)
ls -l file.txt
stat file.txt
Cheat Sheet
touch file.txt # Create file (or update timestamps)
touch file1 file2 file3 # Create/update multiple
touch -a file.txt # Update access time only
touch -m file.txt # Update modification time only
touch -c file.txt # Update time, don’t create if missing
touch -t 202510021200 file.txt # Set specific timestamp (YYYYMMDDhhmm)
touch -d "2025-10-02 12:00:00" f # Set timestamp using a date string
touch -r ref.txt target.txt # Copy timestamp from another file
Mini Quiz
- What happens if you run
touch file.txton an existing file? - Which option prevents new file creation?
- How do you set a custom timestamp with
touch? - How would you copy the timestamp from
index.phptostyle.css?
- Updates timestamps (doesn’t change content). 2)
-c. 3)-t ...or-d "...". 4)touch -r index.php style.css.
Worked Examples (with expected output)
Create a new empty file
touch test.txt
Expected output: (silent success)
Create multiple files
touch file1.txt file2.txt file3.txt
Expected output: (silent success)
Update timestamp of an existing file
touch index.php
Expected output: (silent success)
Update access time only (-a)
touch -a debug.log
Expected output: (silent success)
Update modification time only (-m)
touch -m style.css
Expected output: (silent success)
Do not create if missing (-c)
touch -c not_exist.txt
Expected output: (silent success; no file created)
Set a specific timestamp (-t)
touch -t 202510011230 wp-config.php
Expected output: (silent success)
Set timestamp with human-readable date (-d)
touch -d "2025-09-30 14:00:00" error.log
Expected output: (silent success)
Copy timestamp from another file (-r)
touch -r index.php style.css
Expected output: (silent success)
Verify file creation with ls -l
ls -l test.txt
Expected output (example):
-rw-r--r-- 1 user user 0 Oct 2 09:45 test.txt
Create a hidden file
touch .hiddenfile
Expected output: (silent success)
A .maintenance file in WP root is sometimes used during updates (WordPress itself manages this, but touch is useful for testing).
Update multiple files at once (wildcard)
touch *.php
Expected output: (silent success)
Ensure a daily log exists (cron-friendly)
touch /var/log/wp-daily.log
Expected output: (silent success)
Update both access + modification time explicitly
touch -am functions.php
Expected output: (silent success)
Create a file with today’s date in the name
touch backup-$(date +%Y-%m-%d).sql
Expected output (example filename):
backup-2025-10-02.sql
Touch inside /tmp/
touch /tmp/testfile
Expected output: (silent success)
Touch as sudo (root-owned path)
sudo touch /var/log/secure.log
Expected output: (silent success)
Change timestamp backward (simulate history)
touch -d "2020-01-01 00:00:00" oldfile.txt
Expected output: (silent success)
Create multiple backups with a loop
for i in {1..3}; do touch backup$i.sql; done
Expected output: (silent success)
Verify:
ls backup*.sql
Example output:
backup1.sql
backup2.sql
backup3.sql
Fix “file not found” in scripts (ensure file exists)
touch /var/www/html/wp-content/uploads/missing.log
Expected output: (silent success)